/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nominalitzacions.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author daniel.camacho.rodriguez
 */
public class ChangePasswordView extends JFrame {
    
    private ChangePasswordViewController CPVC;

    private JPanel mainPanel = new JPanel();
    private JPanel allPanel = new JPanel();
    private JPanel buttomPanel = new JPanel();
    private JPanel messagePanel = new JPanel();

    private JButton accept = new JButton("Accept");
    private JButton cancel = new JButton("Cancel");
    private JLabel correctPassMess = new JLabel();
    private JLabel oldPassMess = new JLabel("Old Password :");
    private JLabel newPassMess = new JLabel("New Password :");
    private JLabel reNewPassMess = new JLabel("Rewrite New Password :");
    private JPasswordField oldPass = new JPasswordField();
    private JPasswordField newPass = new JPasswordField();
    private JPasswordField reNewPass = new JPasswordField();

    public ChangePasswordView(ChangePasswordViewController CPVC){
        this.CPVC = CPVC;
        iniComponents();
    }

    public void setVisible() {
        this.pack();
        this.setVisible(true);
    }

    public void setInvisible(){
        this.setVisible(false);
    }

    void openError(String message) {
        correctPassMess.setForeground(new java.awt.Color(240, 0, 0));
        correctPassMess.setText(message);
        accept.setEnabled(false);
        oldPass.setEnabled(false);
        newPass.setEnabled(false);
        reNewPass.setEnabled(false);
        this.pack();
        this.repaint();
    }


    private void actionPerformedOldPassEdit() {
        String act = String.copyValueOf(oldPass.getPassword());
        if (CPVC.verificationPass(act))
        {
            correctPassMess.setForeground(new java.awt.Color(51, 153, 0));
            correctPassMess.setText("Correct Old Password");
            accept.setEnabled(false);
        }
        else
        {
            correctPassMess.setForeground(new java.awt.Color(240, 0, 0));
            correctPassMess.setText("Old Password Incorrect");
            accept.setEnabled(false);
        }
        this.pack();
        this.repaint();
    }

    private void actionPerformedReNewPassEdit() {
        String reAct = String.copyValueOf(reNewPass.getPassword());
        if (reAct.equals(String.copyValueOf(newPass.getPassword())))
        {
            correctPassMess.setForeground(new java.awt.Color(51, 153, 0));
            correctPassMess.setText("All Password are correct");
            accept.setEnabled(true);
        }
        else
        {
            correctPassMess.setForeground(new java.awt.Color(255, 153, 0));
            correctPassMess.setText("New Password Incorrect");
            accept.setEnabled(false);
        }
        this.pack();
        this.repaint();
    }
    
    private void actionPerformedAcceptButtom(ActionEvent event){
        CPVC.passChangeTo(String.copyValueOf(newPass.getPassword()));
        CPVC.closeChangePass();
    }
    
    private void actionPerformedCancelButtom(ActionEvent event){
        CPVC.closeChangePass();
    }


    private void iniComponents() {
        iniFrame();
        iniMainPanel();
        assignListeners();
    }

    private void iniFrame() {
        this.setMinimumSize(new Dimension(287, 180));
        this.setLocationRelativeTo(null);
        this.setPreferredSize(this.getMinimumSize());
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setTitle("Change Password");
        setResizable(false);
        JPanel contentPane = (JPanel) this.getContentPane();
        contentPane.add(mainPanel);
    }

    private void iniMainPanel() {
        accept.setPreferredSize(new Dimension(100, 25));
        accept.setEnabled(false);
        cancel.setPreferredSize(new Dimension(100, 25));

        messagePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        messagePanel.add(correctPassMess);

        buttomPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
        buttomPanel.add(accept);
        buttomPanel.add(cancel);
        
        GroupLayout layout = new GroupLayout(allPanel);
        allPanel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        GroupLayout.SequentialGroup heightGroup = layout.createSequentialGroup();
        heightGroup.addGroup(layout.createParallelGroup()
            .addComponent(oldPassMess)
            .addComponent(newPassMess)
            .addComponent(reNewPassMess));
        heightGroup.addGroup(layout.createParallelGroup()
            .addComponent(oldPass)
            .addComponent(newPass)
            .addComponent(reNewPass));
        layout.setHorizontalGroup(heightGroup);

        GroupLayout.SequentialGroup verGroup = layout.createSequentialGroup();
        verGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
                .addComponent(oldPassMess)
                .addComponent(oldPass));
        verGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
                .addComponent(newPassMess)
                .addComponent(newPass));
        verGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
                .addComponent(reNewPassMess)
                .addComponent(reNewPass));
        layout.setVerticalGroup(verGroup);
        
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(allPanel, BorderLayout.NORTH);
        mainPanel.add(messagePanel, BorderLayout.CENTER);
        mainPanel.add(buttomPanel, BorderLayout.SOUTH);
    }

    private void assignListeners() {

        accept.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedAcceptButtom(event);
            }
        });

        cancel.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedCancelButtom(event);
            }
        });

        oldPass.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                actionPerformedOldPassEdit();
            }
            public void removeUpdate(DocumentEvent e) {
                actionPerformedOldPassEdit();
            }
            public void changedUpdate(DocumentEvent e) {
                actionPerformedOldPassEdit();
            }
        });

        newPass.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                actionPerformedReNewPassEdit();
            }
            public void removeUpdate(DocumentEvent e) {
                actionPerformedReNewPassEdit();
            }
            public void changedUpdate(DocumentEvent e) {
                actionPerformedReNewPassEdit();
            }
        });

        reNewPass.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                actionPerformedReNewPassEdit();
            }
            public void removeUpdate(DocumentEvent e) {
                actionPerformedReNewPassEdit();
            }
            public void changedUpdate(DocumentEvent e) {
                actionPerformedReNewPassEdit();
            }
        });


    }

}
